import QtQuick 2.7
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.1
import QtQuick.Window 2.2
import QtQuick.Controls.Material 2.1
import Han.GetQuestion 1.0
import Han.SubmitQuestion 1.0
Item {
    id:answerdetailwindow
    visible: true
    property real currentQuIndex:0  //其当前题目索引（答题到第几个了）
    property real kid:8      //作业类型id
    property string jsonstr: value
    property real a1:0
    property real a2:0
    property real a3:0
    property real a4:0
     property real a5:0
    property real troubleNum:0;
/////网络模块
GetQuestion{
    id:getQu
    onReceivedqml:{
        var oJson=JSON.parse(str)
        //赋值保存
        answerdetailwindow.jsonstr=str
        //这里在底层转化的时候有一个小错误 总长度是length-1
        if(oJson.length==0)
        {
            //这里后来补上的bug
             console.log("您已经答题了");
             dialog.visible=true;
            dialog.title="您已经答题了";
            dialogcontent.text="您已经答题了";
            otherTabStackView.pop();
             return;
        }
        console.log(str)
        console.log(oJson.length)
        for(var i=0;i<oJson.length-1;i++)
        console.log("完成"+oJson[i].title);

        //--------------显示第一题---------
        ques.text=oJson[0].title;
        an1.text=oJson[0].detail[0].answer;
        an2.text=oJson[0].detail[1].answer;
        an3.text=oJson[0].detail[2].answer;
        an4.text=oJson[0].detail[3].answer;
    }
}

Component.onCompleted: {
    getQu.getQuestsions(window.stuid,answerdetailwindow.kid);
}
SubmitQuestion
{
    id:submitq
    onSigqml: {
        if(answerdetailwindow.troubleNum==0)
        {
            dialog.visible=true;
           dialog.title="提交完成";
           dialogcontent.text="您已经加分！";
           otherTabStackView.pop();
        }else{
            dialog.visible=true;
           dialog.title="提交完成";
           dialogcontent.text="您有答错！"+answerdetailwindow.troubleNum+"道";
           otherTabStackView.pop();
        }
    }
}
/////网络模块结束
    ColumnLayout{
        Label{
            id:ques
            text:"邱于涵这是问题这是问题这是问题这是问题这是问题这是问题"
        }
        RadioButton{
            id:an1
            text:"答案1"
        }
        RadioButton{
            id:an2
            text:"答案2"
        }
        RadioButton{
            id:an3
            text:"答案3"
        }
        RadioButton{
            id:an4
            text:"答案4"
        }
        RowLayout{
            Button{
                id:btnsubmit
                visible:answerdetailwindow.currentQuIndex==4?true:false;
                text:"确认提交"
                onClicked: {
                    var oJson=JSON.parse(answerdetailwindow.jsonstr);
                    //获取最后一个
                    if(an1.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[0].result=="1")answerdetailwindow.a5=1
                    if(an2.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[1].result=="1")answerdetailwindow.a5=1
                    if(an3.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[2].result=="1")answerdetailwindow.a5=1
                    if(an4.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[3].result=="1")answerdetailwindow.a5=1

                    //计算trouble 次数
                    if(answerdetailwindow.a1==0)answerdetailwindow.troubleNum+=1
                    if(answerdetailwindow.a2==0)answerdetailwindow.troubleNum+=1
                    if(answerdetailwindow.a3==0)answerdetailwindow.troubleNum+=1
                    if(answerdetailwindow.a4==0)answerdetailwindow.troubleNum+=1
                    if(answerdetailwindow.a5==0)answerdetailwindow.troubleNum+=1
                    //提交
                submitq.doSubmit(window.stuid,answerdetailwindow.troubleNum,oJson[0].mid,oJson[1].mid,oJson[2].mid,oJson[3].mid,oJson[4].mid,answerdetailwindow.a1,answerdetailwindow.a2,answerdetailwindow.a3,answerdetailwindow.a4,answerdetailwindow.a5);

                //   console.log(answerdetailwindow.a1);
                 //   console.log(oJson[1].mid)
                 //   console.log(oJson[2].mid)
                 //   console.log(oJson[3].mid)
                 //   console.log(oJson[4].mid)
                }

            }
            Button{
                id:btnnext
                visible:answerdetailwindow.currentQuIndex==4?false:true;
                text:"下一题"
                onClicked: {
                    //保存当前的选择
                     var oJson=JSON.parse(answerdetailwindow.jsonstr);
                    if(answerdetailwindow.currentQuIndex==0)
                    {
                        if(an1.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[0].result=="1")answerdetailwindow.a1=1
                        if(an2.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[1].result=="1")answerdetailwindow.a1=1
                        if(an3.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[2].result=="1")answerdetailwindow.a1=1
                        if(an4.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[3].result=="1")answerdetailwindow.a2=1
                    }else if(answerdetailwindow.currentQuIndex==1)
                    {
                        if(an1.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[0].result=="1")answerdetailwindow.a2=1
                        if(an2.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[1].result=="1")answerdetailwindow.a2=1
                        if(an3.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[2].result=="1")answerdetailwindow.a2=1
                        if(an4.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[3].result=="1")answerdetailwindow.a2=1
                    }else if(answerdetailwindow.currentQuIndex==2)
                    {
                        if(an1.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[0].result=="1")answerdetailwindow.a3=1
                        if(an2.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[1].result=="1")answerdetailwindow.a3=1
                        if(an3.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[2].result=="1")answerdetailwindow.a3=1
                        if(an4.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[3].result=="1")answerdetailwindow.a3=1
                    }else if(answerdetailwindow.currentQuIndex==3)
                    {
                        if(an1.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[0].result=="1")answerdetailwindow.a4=1
                        if(an2.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[1].result=="1")answerdetailwindow.a4=1
                        if(an3.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[2].result=="1")answerdetailwindow.a4=1
                        if(an4.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[3].result=="1")answerdetailwindow.a4=1
                    }else if(answerdetailwindow.currentQuIndex==4)
                    {
                        if(an1.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[0].result=="1")answerdetailwindow.a5=1
                        if(an2.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[1].result=="1")answerdetailwindow.a5=1
                        if(an3.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[2].result=="1")answerdetailwindow.a5=1
                        if(an4.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[3].result=="1")answerdetailwindow.a5=1
                    }
                 //   if(an5.checked==true&&oJson[answerdetailwindow.currentQuIndex].detail[4].result=="1")answerdetailwindow.a5=1
                    //下一题
                    ++answerdetailwindow.currentQuIndex;
                    an1.checked=an2.checked=an3.checked=an4.checked=false;
                    //--------------显示下一题---------
                    ques.text=oJson[answerdetailwindow.currentQuIndex].title;
                    an1.text=oJson[answerdetailwindow.currentQuIndex].detail[0].answer;
                    an2.text=oJson[answerdetailwindow.currentQuIndex].detail[1].answer;
                    an3.text=oJson[answerdetailwindow.currentQuIndex].detail[2].answer;
                    an4.text=oJson[answerdetailwindow.currentQuIndex].detail[3].answer;
                }
            }
        }
    }
}
